#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>

#include "oj_model.hpp"  // 文件版
// #include "oj_model2.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";

    class View
    {
    public:
        View() {}
        ~View() {}
    public:
        void AllExpandHtml(const std::vector<Question> &all, std::string *html)
        {
            // 题目编号，题目标题，题目难度
            // 推荐使用表格显示
            // 1. 形成路径
            std::string src_html = template_path + "all_questions.html";    // 待渲染的网页
            // 2. 形成数据字典
            ctemplate::TemplateDictionary root("all_questions"); //根字典
            for(const auto &q : all)
            {
                // 形成子字典
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("questions_list");   
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }
            // 3. 获取被渲染的网页对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            // 4. 渲染网页
            tpl->Expand(html, &root);
        }

        void OneExpandHtml(const Question &q, std::string *html)
        {
            // 1. 形成路径
            std::string src_html = template_path + "one_question.html";    // 待渲染的网页
            // 2. 形成数据字典
            ctemplate::TemplateDictionary root("one_question"); //根字典
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.header);
            // 3. 获取被渲染的网页对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            // 4. 渲染网页
            tpl->Expand(html, &root);
        }
    };
}